const chokidar = require('chokidar');
const exec = require('child_process').exec;

const phpFileExtensions = ['php'];

const watcher = chokidar.watch(phpFileExtensions.map(ext => `**/*.${ext}`), {
    ignored: /(^|[\/\\])\../, // ignore dotfiles
    persistent: true
});


function reload() {
    let cmd = "kill -USR1 `lsof -t -i:9501 | awk 'NR==1'`";

    // 你可以在这里添加你希望执行的操作，例如重启 PHP 服务器等
    exec(cmd, (error, stdout, stderr) => {
        if (error) {
            console.error(`exec error: ${error}`);
            return;
        }
        console.log(`stdout: ${stdout}`);
        console.error(`stderr: ${stderr}`);

    });
}


watcher
    .on('add', (path) => {
        console.log(`File ${path} was added, executing custom action.`);
    })
    .on('change', (path, stats) => {
        console.log(`File ${path} was changed, executing custom action.`);
        reload();
    })
    .on('unlink', (path) => {
        console.log(`File ${path} was removed, executing custom action.`);
    });

// 在此处添加其他需要执行的操作
